home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 February: Tool Chest / Dev.CD Feb 99 TC.toast / Tool Chest / Interapplication Communication / ScriptableStuffItEngine / Project / SSE_HandleStuffEvent.cp < prev    next >
Encoding:
Text File  |  1998-12-02  |  3.3 KB  |  154 lines  |  [TEXT/CWIE]

  1. /*
  2.     You may incorporate this Apple sample source code into your program(s) without
  3.     restriction. This Apple sample source code has been provided "AS IS" and the
  4.     responsibility for its operation is yours. You are not permitted to redistribute
  5.     this Apple sample source code as "Apple sample source code" after having made
  6.     changes. If you're going to re-distribute the source, we require that you make
  7.     it clear in the source that the code was descended from Apple sample source
  8.     code, but that you've made changes.
  9. */
  10.  
  11. #include "ScriptableStuffItEngine.h"
  12.  
  13. #ifndef __StuffItLib__
  14. #    include "StuffItEngineLib.h"
  15. #endif
  16.  
  17. #ifndef __ALIASES__
  18. #    include <Aliases.h>
  19. #endif
  20.  
  21. #ifndef __ERRORS__
  22. #    include <Errors.h>
  23. #endif
  24.  
  25. static pascal OSErr ReplyToStuff (const FSSpec *actualArchive, AppleEvent *reply)
  26. {
  27.     OSErr err = noErr;
  28.  
  29.     AliasHandle aliasH = nil;
  30.  
  31.     if (!(err = NewAliasMinimal (actualArchive,&aliasH)))
  32.     {
  33.         HLockHi (Handle (aliasH));
  34.         Size handleSize = GetHandleSize (Handle (aliasH));
  35.         err = AEPutParamPtr (reply,keyDirectObject,typeAlias,*aliasH,handleSize);
  36.         DisposeHandle (Handle (aliasH));
  37.     }
  38.  
  39.     return err;
  40. }
  41.  
  42. static pascal OSErr GetFilesToStuff (const AppleEvent *ae, FSSpecArrayHandle *stuffItArrayH)
  43. {
  44.     AEDescList listToStuff;
  45.     OSErr err = AEGetParamDesc (ae,keyDirectObject,typeAEList,&listToStuff);
  46.  
  47.     if (!err)
  48.     {
  49.         OSErr err2;
  50.  
  51.         *stuffItArrayH = NewFSSpecList ( );
  52.  
  53.         if (!*stuffItArrayH)
  54.             err = memFullErr;
  55.         else
  56.         {
  57.             long index;
  58.  
  59.             err = AECountItems (&listToStuff,&index);
  60.  
  61.             if (!err) while (index)
  62.             {
  63.                 FSSpec        fss;
  64.                 AEKeyword    keyWord;
  65.                 DescType    typeCode;
  66.                 Size        actualSize;
  67.  
  68.                 err = AEGetNthPtr
  69.                     (&listToStuff,index,typeFSS,&keyWord,&typeCode,&fss,sizeof(fss),&actualSize);
  70.                 if (err) break;
  71.                 err = AddToFSSpecList (&fss,*stuffItArrayH);
  72.                 if (err) break;
  73.                 --index;
  74.             }
  75.  
  76.             if (err)
  77.             {
  78.                 DisposeFSSpecList (*stuffItArrayH);
  79.                 *stuffItArrayH = nil;
  80.             }
  81.         }
  82.  
  83.         err2 = AEDisposeDesc (&listToStuff);
  84.         if (!err) err = err2;
  85.     }
  86.  
  87.     return err;
  88. }
  89.  
  90. static pascal OSErr GetRequestedArchive (const AppleEvent *ae, FSSpecPtr *fssP)
  91. {
  92.     OSErr err = noErr;
  93.  
  94.     *fssP = FSSpecPtr (NewPtr (sizeof (**fssP)));
  95.  
  96.     if (!*fssP)
  97.         err = MemError ( );
  98.     else
  99.     {
  100.         DescType    actualType;
  101.         Size        actualSize;
  102.  
  103.         err = AEGetParamPtr (ae,keyTargetAlias,typeFSS,&actualType,*fssP,
  104.             sizeof (**fssP),&actualSize);
  105.  
  106.         if (err)
  107.         {
  108.             DisposePtr (Ptr (*fssP));
  109.             *fssP = nil;
  110.  
  111.             if (err == errAEDescNotFound)
  112.                 err = noErr;
  113.         }
  114.     }
  115.  
  116.     return err;
  117. }
  118.  
  119. pascal OSErr Stuff (const AppleEvent *ae, AppleEvent *reply, long magicCookie)
  120. {
  121.     FSSpecArrayHandle stuffItArrayH;
  122.     OSErr err = GetFilesToStuff (ae,&stuffItArrayH);
  123.  
  124.     if (!err)
  125.     {
  126.         FSSpecPtr requestedArchive = nil;
  127.  
  128.         if (!(err = GetRequestedArchive (ae,&requestedArchive)))
  129.         {
  130.             Boolean deleteOriginals = false;
  131.  
  132.             if (!(err = GetOptionalBoolean (ae,&deleteOriginals,keyDeleteOriginals)))
  133.             {
  134.                 FSSpec actualArchive;
  135.  
  136.                 err = StuffFSList (magicCookie,stuffItArrayH,requestedArchive,
  137.                     &actualArchive,true,deleteOriginals,false,false,true,kStuffConflictPrompt);
  138.  
  139.                 if (!err)
  140.                     err = ReplyToStuff (&actualArchive, reply);
  141.                 else if (err == -1) // why don't they return userCanceledErr?
  142.                     err = userCanceledErr;
  143.             }
  144.  
  145.             if (requestedArchive)
  146.                 DisposePtr (Ptr (requestedArchive));
  147.         }
  148.  
  149.         DisposeFSSpecList (stuffItArrayH);
  150.     }
  151.  
  152.     return err;
  153. }
  154.